//
//  MoreLoanBankView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/1.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol MoreLoanBankViewDelegate {
    @objc optional func selectLoanBankType(title:String) //选择贷款银行
}

class MoreLoanBankView: UIView {
    
    weak var delegate : MoreLoanBankViewDelegate?
    
    fileprivate let cellId = "moreLoanBankCell"
    fileprivate let dataArray = [unlimitedBank,CITICBank,PufaBank,GuangfaBank,EverbrightBank,PeaceBank,CitiBank,GuangzhouBank,ZheshangBank,ChinaMerchantsBank,BankofCommunications,MinshengBank,IndustrialBank,HSBCBank,ShanghaiBank,BankofChina,ABC,ICBC,Postalsaving,Constructionbank,BaoshangBank,BankofBeijing,ChongqingBank,HSBC,StandardCharteredBank,PinganHewlett]
    
    fileprivate var collectionView: UICollectionView!

    init() {
        super.init(frame: .zero)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = .init(width: ScreenW/4, height: RATIO_H(maxNum: 40))
        
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(MoreLoanBankCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension MoreLoanBankView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? MoreLoanBankCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        
        if indexPath.row == 0 {
            cell?.titleLabel.backgroundColor = UIColor.red
            cell?.titleLabel.textColor = UIColor.white
        }else{
            cell?.titleLabel.backgroundColor = UIColor.clear
            cell?.titleLabel.textColor = UIColor.darkGray
        }
        
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.row != 0 {
            let firstCell = collectionView.cellForItem(at: NSIndexPath.init(row: 0, section: 0) as IndexPath) as? MoreLoanBankCell
            firstCell?.titleLabel.backgroundColor = UIColor.clear
            firstCell?.titleLabel.textColor = UIColor.darkGray
        }
     
        let cell = collectionView.cellForItem(at: indexPath) as? MoreLoanBankCell
        cell?.titleLabel.backgroundColor = UIColor.red
        cell?.titleLabel.textColor = UIColor.white
        
        self.delegate?.selectLoanBankType!(title: dataArray[indexPath.row])
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        
        let cell = collectionView.cellForItem(at: indexPath) as? MoreLoanBankCell
        cell?.titleLabel.backgroundColor = UIColor.clear
        cell?.titleLabel.textColor = UIColor.darkGray
    }
}
